#include <QApplication>
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include "pagedquerywidget.h"
#include "testdata.h"
#include <QDebug>

// 确保配置文件存在
bool ensureConfigFile(const QString &configPath)
{
    // 如果文件已存在，直接返回成功
    if (QFile::exists(configPath)) {
        return true;
    }

    // 创建目录
    QDir dir;
    dir.mkpath(QFileInfo(configPath).path());

    // 创建默认配置文件
    QFile file(configPath);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        return false;
    }

    // 写入默认配置
    QByteArray defaultConfig = R"({
  "filters": [
    {
      "name": "日期范围",
      "type": "daterange",
      "field": "date"
    },
    {
      "name": "关键字",
      "type": "text",
      "field": "keyword"
    },
    {
      "name": "状态",
      "type": "combobox",
      "field": "status",
      "options": ["全部", "待处理", "处理中", "已处理", "已取消"]
    }
  ],
  "columns": [
    {
      "name": "ID",
      "field": "id",
      "width": 80,
      "filterable": false,
      "sortable": true,
      "interactive": true
    },
    {
      "name": "日期",
      "field": "date",
      "width": 120,
      "filterable": true,
      "filterType": "daterange",
      "sortable": true,
      "interactive": false
    },
    {
      "name": "标题",
      "field": "title",
      "width": 300,
      "filterable": true,
      "filterType": "text",
      "sortable": true,
      "interactive": true
    },
    {
      "name": "状态",
      "field": "status",
      "width": 100,
      "filterable": true,
      "filterType": "combobox",
      "filterOptions": ["全部", "待处理", "处理中", "已处理", "已取消"],
      "sortable": true,
      "interactive": false
    },
    {
      "name": "操作",
      "field": "actions",
      "width": 150,
      "filterable": false,
      "sortable": false,
      "interactive": true
    }
  ],
  "pagination": {
    "defaultPageSize": 10,
    "pageSizeOptions": [10, 20, 50, 100]
  }
})";

    file.write(defaultConfig);
    file.close();

    return true;
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // 配置文件路径
    QString configPath = "config/query_config.json";

    // 确保配置文件存在
    if (!ensureConfigFile(configPath)) {
        QMessageBox::critical(nullptr, "错误", "无法创建配置文件");
        return -1;
    }

    // 创建分页查询组件
    PagedQueryWidget queryWidget(configPath);
    queryWidget.setWindowTitle("分页查询示例");

    // 创建测试数据
    TestData testData;

    // 初始加载数据
    queryWidget.setData(testData.getData(1, 10, QMap<QString, QVariant>()),
                        testData.getTotalCount(QMap<QString, QVariant>()));

    // 连接信号槽
    QObject::connect(&queryWidget, &PagedQueryWidget::pageChanged,
                     [&](int page, int pageSize) {
        qDebug()<<pageSize;
        QMap<QString, QVariant> filters = queryWidget.getFilterValues();
        QString sortField = queryWidget.getSortField();
        Qt::SortOrder sortOrder = queryWidget.getSortOrder();

        queryWidget.setData(testData.getData(page, pageSize, filters, sortField, sortOrder),
                           testData.getTotalCount(filters));
    });

    QObject::connect(&queryWidget, &PagedQueryWidget::filterChanged,
                     [&](const QMap<QString, QVariant> &filters) {
        int pageSize = queryWidget.getPageSize();
        QString sortField = queryWidget.getSortField();
        Qt::SortOrder sortOrder = queryWidget.getSortOrder();

        queryWidget.setData(testData.getData(1, pageSize, filters, sortField, sortOrder),
                           testData.getTotalCount(filters));
    });

    QObject::connect(&queryWidget, &PagedQueryWidget::sortChanged,
                     [&](const QString &field, Qt::SortOrder order) {
        int page = queryWidget.getCurrentPage();
        int pageSize = queryWidget.getPageSize();
        QMap<QString, QVariant> filters = queryWidget.getFilterValues();

        queryWidget.setData(testData.getData(page, pageSize, filters, field, order),
                           testData.getTotalCount(filters));
    });

    QObject::connect(&queryWidget, &PagedQueryWidget::cellClicked,
                     [&](int row, int column, const QString &field, const QVariant &value) {
        if (field == "id" || field == "title") {
            QMessageBox::information(nullptr, "详情",
                                    QString("查看记录详情\n行: %1\n列: %2\n字段: %3\n值: %4")
                                    .arg(row).arg(column).arg(field).arg(value.toString()));
        } else if (field == "actions") {
            QMessageBox::information(nullptr, "操作",
                                    QString("执行操作\n行: %1\n列: %2\n字段: %3")
                                    .arg(row).arg(column).arg(field));
        }
    });

    queryWidget.show();

    return app.exec();
}
